package com.learn.project2.service.admin.address;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketAddress;
import com.learn.project2.bean.MarketAddressExample;
import com.learn.project2.mapper.MarketAddressMapper;
import com.learn.project2.model.bo.admin.user.UserListBO;
import com.learn.project2.model.vo.admin.address.AddressListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @description: 收货地址
 * @author wxy
 * @date 2022-01-09 20:44
 * @version 1.0
 */
//@CacheConfig(cacheNames = "marketAddress")
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    MarketAddressMapper addressMapper;
    @Override
    //@Cacheable
    public AddressListVO selectAddress(String name, Integer userId, UserListBO userListBO) {
        Integer page = userListBO.getPage();
        Integer limit = userListBO.getLimit();
        String sort = userListBO.getSort();
        String order = userListBO.getOrder();
        //使用分页插件
        PageHelper.startPage(page,limit);
        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();

        //name模糊匹配和userId精确匹配
        if (name != null){
            criteria.andNameLike("%" + name + "%");}
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        criteria.andDeletedEqualTo(false);
        addressExample.setOrderByClause(sort+" "+order);
        List<MarketAddress> list = addressMapper.selectByExample(addressExample);

        PageInfo<MarketAddress> pageInfo = new PageInfo<>(list);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        return AddressListVO.data(total,pages,limit,page,list);
    }
}
